package cn.com.gridinfo.sooc.core.expand.callname.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.jeremy.arad.utils.Log;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocFragment;
import cn.com.gridinfo.sooc.core.expand.callname.activity.SignInfoActivity;
import cn.com.gridinfo.sooc.core.expand.callname.adapter.ShiDaoAdapter;
import cn.com.gridinfo.sooc.core.expand.callname.bean.ClassInfo;
import cn.com.gridinfo.sooc.core.expand.callname.bean.Student;
import cn.com.gridinfo.sooc.core.expand.callname.common.CodeConst;
import cn.com.gridinfo.sooc.core.expand.callname.common.PageConst;
import cn.com.gridinfo.sooc.core.expand.callname.dao.CallNameDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 实到 列表
 * Created by yingchunli on 2015/12/23.
 */
public class ShiDaoListFragment extends BaseSoocFragment {

    View view;//当前界面
    @InjectView(R.id.list_sign_shidao)
    UltimateRecyclerView signinfoList;//列表
    LinearLayoutManager linearLayoutManager;//布局管理器
    ShiDaoAdapter adapter;//适配器
    int maxLastPositon;//数据最后位置
    String ktid;//课堂id
    CallNameDao dao;
    int position;//设为缺席的学生的位置
    String kcid;//课程id

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.expand_signinfo_shidao_list, null);
        ButterKnife.inject(this, view);
        dao = new CallNameDao(this);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        signinfoList.setLayoutManager(linearLayoutManager);
        Bundle bundle = getArguments();
        ktid = bundle.getString("ktid");
        kcid = bundle.getString("kcid");
        initView();
        initData();

        return view;
    }


    /**
     * 加载数据
     */
    void initData() {
        PageConst.pageNo = 1;
        PageConst.pageSize = 10;
        dao.LookClassInfo(ktid, PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_SIGNINFO_SHIDAO_PUSHDOWN);
    }

    /**
     * 列表无数据，空白页的设置
     */
    void setBlankView(List<Student> list) {
        Log.d("无数据显示");
        RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.blank_view);
        layout.setVisibility(View.VISIBLE);
        TextView textView = (TextView) view.findViewById(R.id.tipBlank);//提示文字
        textView.setText("目前没有学生签到，稍后再来查看~");
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getPicpath() != null || list.get(i).getPicpath().equals("")) {
                ShiDaoFragment.photoContainer.setVisibility(View.VISIBLE);
                return;
            } else {
                ShiDaoFragment.photoContainer.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.getStatus().equals("0")) {
                switch (requestCode) {
//                    case CodeConst.EXPAND_SIGNINFO_SHIDAO_FRIST:
//                        //获取签到详情成功  首次请求
//                        initView();
//                        if (Integer.parseInt(dao.getClassInfo().getQdrs()) > 0) {
//
//                            ShiDaoFragment.countRen.setText(dao.getClassInfo().getQdrs());
//                        } else {
//                            setBlankView(dao.getStudentList());
//                        }
//                        break;
                    case CodeConst.EXPAND_SIGNINFO_SHIDAO_PUSHDOWN://获取列表数据  下拉请求
                        signinfoList.setRefreshing(false);
                        if (dao.getStudentList() != null && !dao.getStudentList().isEmpty()) {
                            signinfoList.hideEmptyView();
                            List<Student> list = new ArrayList<>();
                            for (int i = 0; i < dao.getStudentList().size(); i++) {
                                if (dao.getStudentList().get(i).getQdzt().equals("1")) {
                                    list.add(dao.getStudentList().get(i));
                                }
                            }
                            adapter.refresh(list);
                            //设置实到人数
                            ShiDaoFragment.countRen.setText(dao.getClassInfo().getQdrs());
                            adapter.loadNoDataView();
                        } else {
                            //无数据处理
                            setBlankView(dao.getStudentList());
                        }
//                        if (dao.getStudentList().size() > (PageConst.pageSize - 1)) {
//                            adapter.loadMoreDataView();
//                        } else {
                            //数据小于当前页的话则不显示加载更多

//                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
//                            linearLayoutManager.scrollToPosition(maxLastPositon);
//                        }
                        break;
                    case CodeConst.EXPAND_SIGNINFO_SHIDAO_PULLUP://获取列表数据  上拉请求
                        if (dao.getStudentList() != null && !dao.getStudentList().isEmpty()) {
                            adapter.loadmore(dao.getStudentList());
                        } else {
                            //无数据则不显示下一页
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                        }
                        break;
                    case CodeConst.EXPAND_BUQIAN://设为缺席
                        MessageUtils.showRightImageShortToast(getActivity(), "设为缺席成功");
                        //设置签到详情界面 的实到人数
                        int count = Integer.parseInt(ShiDaoFragment.countRen.getText().toString().trim());
                        ShiDaoFragment.countRen.setText(count - 1 + "");
                        adapter.delete(position);//更新列表
                        break;
                }
            } else {
                MessageUtils.showImageShortToast(getActivity(), dao.getStatusCode());
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(getActivity(), "请求数据异常");
        }
    }

    /**
     * 初始化界面数据
     */
    void initView() {
        //去除签到状态为2的学生，保留签到状态为1的学生
        List<Student> list = new ArrayList<>();
        adapter = new ShiDaoAdapter(list, getActivity());
        signinfoList.setAdapter(adapter);
        signinfoList.enableLoadmore();
        signinfoList.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                PageConst.pageNo = 1;//首页
                dao.LookClassInfo(ktid, PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_SIGNINFO_SHIDAO_PUSHDOWN);
                linearLayoutManager.scrollToPosition(0);
            }
        });
        signinfoList.setOnLoadMoreListener(new UltimateRecyclerView.OnLoadMoreListener() {
            @Override
            public void loadMore(final int itemsCount, final int maxLastVisiblePosition) {

//                if (itemsCount > (PageConst.pageSize - 1)) {
//                    maxLastPositon = maxLastVisiblePosition;
//                    dao.LookClassInfo(ktid, ++PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_SIGNINFO_SHIDAO_PULLUP);
//                } else {
//                    if (signinfoList != null)
//                        adapter.loadNoDataView();
//                    linearLayoutManager.scrollToPositionWithOffset(maxLastVisiblePosition, -1);
//                }
            }
        });
        //适配器的单项点击事件
        adapter.setmOnItemClickLitener(new ShiDaoAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, final Student student, int position1) {
                //将学生设为缺席
                Log.d("xh->" + student.getXh());
                position = position1;

                new MaterialDialog.Builder(getActivity())
                        .items(R.array.setting_quexi)
                        .backgroundColor(getResources().getColor(R.color.white))
                        .contentColor(getResources().getColor(R.color.color_tv333))
                        .itemsCallback(new MaterialDialog.ListCallback() {
                            @Override
                            public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                                //设为缺席
                                dao.buQian(ktid, kcid, student.getXh(), "0", CodeConst.EXPAND_BUQIAN);
                            }
                        })
                        .show();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
